@extends('admin.pub.pub')
@section('title','角色列表')
@section('bread1','角色管理')
@section('bread2','角色授权')
@section('css')
    <link rel="stylesheet" href="/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="/bootstrap/css/bootstrap-theme.css">
@endsection
@section('content')
    <div class="container">
        <form role="form">
            <div class="form-group-sm">
                <label for="rolename" class="col-sm-4 control-label">角色名称</label>
                <div class="col-sm-8">
                    <input type="text" class="input-text" name="name" value="{{ $data->name }}" id="rolename" readonly disabled>
                </div>
            </div>
            <div class="col-sm-12">权限：</div>
            @foreach($permissions as $permission)
                <div class="form-group-sm">
                    <div class="col-sm-{{ 12-$permission['lv']+1 }} col-offset-{{ $permission['lv']-1 }}">
                        <div class="checkbox
                        @foreach($data->permissions as $item)
                        @if($item->id == $permission['id'])
                                checked
                        @endif
                        @endforeach
                        ">
                            <label>
                                <input type="checkbox"
                                       @foreach($data->permissions as $item)
                                       @if($item->id == $permission['id'])
                                               checked
                                       @endif
                                       @endforeach
                                       id="permission_{{ $permission['id'] }}"
                                       value="{{ $permission['id'] }}"
                                       name="permissions[]">
                                <label for="permission_{{ $permission['id'] }}">{{ $permission['name'] }}</label>
                            </label>
                        </div>
                    </div>
                </div>
            @endforeach
            <button id="edit" class="btn btn-primary radius">修改</button>
        </form>
    </div>
@endsection
@section('js')
    <script type="text/javascript" src="/admin/static/h-ui/js/H-ui.js"></script>
    <script>
		$('.checkbox input:checkbox').iCheck({
			checkboxClass: 'icheckbox-blue',
			radioClass: 'iradio-blue',
			increaseArea: '20%'
		})
        $('#edit').click(() => {
        	var inputs = $('.checkbox input:checkbox:checked');
        	var permissions = [];
        	$.each(inputs,function(i,val){
        		permissions.push(val.value)
            });
	        var _token = '{{ csrf_token() }}';
            $.ajax({
                url:'{{ route('admin.role.update',['id'=>$data->id]) }}',
                type:'PUT',
                data:{permissions,_token},
                dataType:'json',
                success(res) {
	                var index = parent.layer.getFrameIndex(window.name);
	                parent.layer.close(index);
                }
            });
            return false;
        })
    </script>
@endsection
